"""
Case Type   : SQL_LIMIT
Case Name   : 设置enable_sql_limit = off，使用具有sysadmin权限的用户创建规则，合理报错
Create At   : 2025/9/16
Owner       : lonely-dance
Description :
    1、修改参数enable_sql_limit=off;
    2、创建sql限流limit
Expect      :
    1、修改成功
    2、合理报错
History     :
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class SqlLimit(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----- {os.path.basename(__file__)} start-----')
        self.comm_sh = CommonSH('PrimaryDbUser')
        self.Constant = Constant()
        self.limit_name1 = 'limit0005_01'

    def test_main(self):
        text = '----step1:修改参数enable_sql_limit=off expect:成功----'
        self.log.info(text)
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'enable_sql_limit = off')

        text = '----step2:创建sql限流limit expect:合理报错----'
        self.log.info(text)
        cmd = f'''
        select gs_create_sql_limit('{self.limit_name1}', 'delete', 0, 100, 'now()', 
        '2025-3-30 23:59:59', '{{{{delete from}}}}', '', '');'''
        self.log.info(cmd)
        res = self.comm_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertIn('ERROR:  enable_sql_limit is off, please set to on', res, '报错信息异常')

    def tearDown(self):
        text = '----无需恢复环境----'
        self.log.info(text)
        self.log.info(f'----- {os.path.basename(__file__)} end-----')
